Skip to content

test(api): bring the billing service tests under strict pyrefly - #46407

Open
MindOfqc wants to merge 9 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-billing-service
Open

MindOfqc wants to merge 9 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-billing-service

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Part of the pyrefly strict-mode migration: this file leaves project-excludes.

api/tests/unit_tests/pyrefly.toml now carries real annotations, and the remaining diagnostics it exposed were fixed rather than suppressed. No pyrefly: ignore / type: ignore / cast / typing.Any was added.

Verification: pyrefly check on tests/unit_tests/pyrefly.toml reports 29 errors (all pre-existing missing-import for optional packages) and 0 warnings with 0 diagnostics left in the target file; pytest passes match the pre-change baseline; ruff check and ruff format --check pass.

Out-of-vocabulary freeze types are injected through model_copy, which skips
validation the same way the upstream response path does, instead of widening the
field to a plain string.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 14, 2026
@MindOfqc MindOfqc changed the title test(api) test(api): bring the billing service tests under strict pyrefly Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-26 14:03:52.266339810 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-26 14:03:41.254266297 +0000
@@ -5067,8 +5067,6 @@
    --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:760:16
 ERROR `in` is not supported between `Literal['not found']` and `None` [not-iterable]
    --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:894:16
-ERROR Argument `str` is not assignable to parameter `freeze_type` with type `Literal['', 'email_domain_suspended', 'freeze']` in function `services.billing_client.saas.CheckAccountInFreezeReply.model_construct` [bad-argument-type]
-   --> tests/unit_tests/services/test_billing_service.py:373:87
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `ConversationStatus | SQLCoreOperations[ConversationStatus]` in function `models.model.Conversation.__init__` [bad-argument-type]
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:117:16
 ERROR Object of class `object` has no attribute `id` [missing-attribute]

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.92% 87.94% +0.03%
Strict coverage 87.58% 87.61% +0.03%
Typed symbols 97,241 97,282 +41
Untyped symbols 13,415 13,389 -26
Modules 4672 4672 0

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.52%. Comparing base (df05396) to head (cb631cb).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46407      +/-   ##
==========================================
- Coverage   90.53%   90.52%   -0.01%     
==========================================
  Files        6608     6608              
  Lines      410585   410691     +106     
  Branches    82396    82394       -2     
==========================================
+ Hits       371706   371781      +75     
- Misses      32489    32512      +23     
- Partials     6390     6398       +8     
Flag Coverage Δ
api 90.20% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
… tests

`CheckAccountInFreezeReply.freeze_type` was generated as a closed `Literal`, but the
value comes from a proto enum, so a dify-saas deployment older or newer than the
committed snapshot can send a member the spec never listed. The only consumer,
`BillingService.get_email_freeze_type`, already treats such a value as valid (`case _`),
so the literal only moved the failure from that fallback into pydantic validation and
made it log to Sentry on every reply from an older server.

The generated model now declares `str`, and the test builds a real
`CheckAccountInFreezeReply` through validation instead of `model_construct` plus
`model_copy`. Since `saas.py` is a generated artefact guarded by `--check`, the change
lands in `generate_billing_client.py` as an explicit allowlist of open enum fields,
with a check that fails generation if an entry stops naming an enum field or disappears
from the snapshot.

Verified: `generate_billing_client.py --check` exits 0, the strict pyrefly run stays at
the 29 missing-import baseline with no new warnings, `test_billing_service.py` passes 30
tests, and ruff is clean.
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 15, 2026
@MindOfqc

Copy link
Copy Markdown
Contributor Author

Fixed at the origin as suggested — and since saas.py is generated, the fix belongs in the generator.

CheckAccountInFreezeReply.freeze_type is rendered from a proto enum, but the set is open: a dify-saas deployment older or newer than the committed snapshot can send a member the spec never listed. The only consumer, BillingService.get_email_freeze_type, already handles that with case _, so the literal did not protect anything — it only moved the failure from that fallback into pydantic, and transport.py logs validation failures to Sentry while salvaging with model_construct. So every reply from an older server was both noise and a validation bypass.

generate_billing_client.py now carries an explicit allowlist of open enum fields, and a check_open_enum_fields pass fails generation if an entry stops naming an enum field or vanishes from the snapshot. saas.py declares str, the snapshot JSON is untouched, and the _freeze_reply helper is gone — the test builds a real model through validation, including the "unexpected" case, which is the point of the change.

Verified: generate_billing_client.py --check exits 0, git diff --stat saas.py is non-empty, strict pyrefly stays at the 29 missing-import baseline with no new warnings, the production project is unchanged at its 3174/32 baseline, test_billing_service.py passes 30 and the billing suites pass 101, ruff is clean, and all seven check_no_new_* guards exit 0.

The allowlist only earns its place if a stale entry is caught, so the new tests pin
both directions: an entry that stops naming an enum field and an entry that no longer
appears in the snapshot each fail generation. The rendering tests also fix the contract
that an open enum field becomes `str` while nullability and presence rules still apply,
which is what lets the billing client accept a value the committed spec never listed.
… helper

`BillingService.get_model_provider_payment_link` was decorated with
`warnings.deprecated`, so both its one product caller and its unit test had
to carry a `# pyrefly: ignore [deprecated]` comment. The endpoint it serves
is already marked deprecated in the console OpenAPI surface, and a route
that still exists cannot be "migrated away from" by a warning.

Remove the decorator and its now unused `warnings.deprecated` import, and
record the status in the helper's docstring instead. Both ignores go away
with the diagnostic.

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 inline suggestions at 13c3a9611c, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/dev/generate_billing_client.py Resolved
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn label Nov 16, 2026
class CheckAccountInFreezeReply(BillingModel):
data: bool
freeze_type: Literal["", "freeze", "email_domain_suspended"] = Field(alias="freezeType")
freeze_type: str = Field(alias="freezeType")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this changed from literal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

freezeType is an open proto enum, not a closed one. A dify-saas deployment older or newer than this committed spec snapshot can return a value the spec never listed. With Literal[...], pydantic would reject that reply with a ValidationError even though the consumer can still read and act on it — turning a harmless new enum value into a spurious error in Sentry.

The consumer of CheckAccountInFreezeReply.freeze_type already matches with a case _ fallback, so widening to str loses no safety. The change is made in the generator (api/dev/generate_billing_client.py), not by hand-editing the generated saas.py (api/AGENTS.md forbids editing generated files): a minimal OPEN_ENUM_FIELDS allowlist keyed by (schema, wire name) renders just this one field as str. The allowlist comment says to keep it minimal and to drop the entry once the upstream spec removes the enum.

Every other enum field stays Literal — only fields whose consumer already handles unknown values are widened.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 17, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 19, 2026
Resolve the project-excludes conflict by three-way comparison: keep entries each side still excludes, drop entries upstream graduated, append entries upstream newly excluded. Result 140 entries.
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 22, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 26, 2026
# Conflicts:
#	api/tests/unit_tests/pyrefly.toml

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline suggestion at be40ea9da3, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 1 review thread is still unresolved.

dify-codereview

Comment thread api/dev/generate_billing_client.py Resolved
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 26, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants